package demo15_Sync;

import java.sql.Time;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * @author kunkun
 * @className Test01_Sync
 * @Description 测试异步执行，拥有返回值
 * @date 2025/3/7 14:37
 */
public class Test02_Sync {
    public static void main(String[] args) throws InterruptedException, ExecutionException {

        //1. 执行异步请求，拥有返回值类型
        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行异步请求");
            return 1024;
        });

        //2. 主线程执行
        TimeUnit.SECONDS.sleep(1);
        System.out.println("主线程开始执行");

        //3. 获得返回值
        System.out.println("最后的结果："+completableFuture.whenComplete((t, u) -> {
            System.out.println("t:" + t); //返回值
            System.out.println("u:" + u); //异常
        }).get());  //输出返回值


    }
}
